package com.cykj.pos.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.core.domain.entity.SysDictData;
import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.common.utils.SecurityUtils;
import com.cykj.pos.domain.*;
import com.cykj.pos.domain.dto.*;
import com.cykj.pos.enums.bizstatus.BizStatusContantEnum;
import com.cykj.pos.profit.dto.BizCashoutRecordsDTO;
import com.cykj.pos.profit.dto.MessageDTO;
import com.cykj.pos.profit.dto.WalletDTO;
import com.cykj.pos.service.*;
import com.cykj.pos.util.LoginUserUtils;
import com.cykj.system.service.ISysDictDataService;
import com.cykj.system.service.ISysUserService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *消息管理Controller
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pos/api/v2/message")
public class PosV2AppMessageController {

    private final IBizMessageRecordsService messageRecordsService;

    private final IBizEarningsService bizEarningsService;

    private final IBizMerchantService merchantService;

    private final IBizMerchBillService bizMerchBillService;

    private final IBizAllocAdjRecordsService bizAllocAdjRecordsService;

    private final IBizCashoutRecordsService bizCashoutRecordsService;

    private final IBizMerchIntegralService merchIntegralService;


    /**
     * 需要传递userId和msgType
     * 通过用户id和消息类型查找消息列表接口
     * @param messageDTO
     * @return
     */
    @PostMapping("/list")
    public AjaxResult list(@RequestBody MessageDTO messageDTO){
        AjaxResult ajaxResult = AjaxResult.success("我的消息列表数据获取成功");
        // 获取消息列表
        List<MessageDTO> messageList = messageRecordsService.getMessageListByUserIdAndType(messageDTO);
        ajaxResult.put("data",messageList);
        return ajaxResult;
    }

    /**
     * 获得单个消息  传入一个msgId
     * @param messageDTO
     * @return
     */
    @PostMapping("/detail")
    public AjaxResult detail(@RequestBody MessageDTO messageDTO){
        AjaxResult ajaxResult = AjaxResult.success("我的消息数据获取成功");
        // 获取单个消息列表
        MessageDTO message = messageRecordsService.getMessageByMgsId(messageDTO);
        ajaxResult.put("data",message);
        return ajaxResult;
    }
    /**
     * 获得单个消息  传入一个msgId
     * @param messageDTO
     * @return
     */
    @PostMapping("/msgTypeDetail")
    public AjaxResult typeDetail(@RequestBody MessageDTO messageDTO){
        AjaxResult ajaxResult = AjaxResult.success("我的消息数据获取成功");
        // 消息类型  1-入库 2-机具激活 3- 预约提现成功 4-提现 5-提现失败 6-兑换申请 7-兑换 8-返积分
        if(messageDTO.getMsgType()==1){ //入库
            MsgAllocAdjRecordsDTO msgAllocAdjRecordsDTO = bizAllocAdjRecordsService.getAllocAdjRecordsListByMsgId(messageDTO);
            ajaxResult.put("data",msgAllocAdjRecordsDTO);
        }else if(messageDTO.getMsgType()==4){ //4 提现成功
            MsgCashoutRecordDTO msgCashoutRecordDTO = bizCashoutRecordsService.getCashoutRecordByMsgId(messageDTO);
            ajaxResult.put("data",msgCashoutRecordDTO);
        }else if(messageDTO.getMsgType()==5){ //5 提现失败
            MsgCashoutRecordDTO msgCashoutRecordDTO = bizCashoutRecordsService.getCashoutRecordByMsgId(messageDTO);
            ajaxResult.put("data",msgCashoutRecordDTO);
        }else if(messageDTO.getMsgType()==2){ // 激活返现成功
            MsgMerchBillDTO msgMerchBillDTO = bizMerchBillService.getMerchBillByMsgId(messageDTO);
            ajaxResult.put("data",msgMerchBillDTO);
        }else if(messageDTO.getMsgType()==8){ //8-返积分
            MsgMerchIntegralDTO msgMerchIntegralDTO = merchIntegralService.getMerchIntegralByMsgId(messageDTO);
            ajaxResult.put("data",msgMerchIntegralDTO);
        }
        return ajaxResult;
    }

    @PostMapping("/earnings")
    public AjaxResult earnings(@RequestBody MessageDTO messageDTO){
        AjaxResult ajaxResult = AjaxResult.success("收益信息获取成功");
        BizMerchant merchant = merchantService.getMerchantByUserId(messageDTO.getUserId()); //商户信息
        messageDTO.setMsgUserId(merchant.getMerchId());
        // 获取收益信息
        List<BizEarnings> list = bizEarningsService.queryEarningsList(messageDTO);
        for (int i=0;i<list.size();i++){
            String date = list.get(i).getCreateTime();
            String nowDate = date.substring(0,7);
            list.get(i).setCreateTime(nowDate);
            list.get(i).setOrderTime(list.get(i).getOrderTime());
        }
        ajaxResult.put("data",list);
        return ajaxResult;
    }

    @PostMapping("/billDetails")
    public AjaxResult billDetails(@RequestBody BizMerchBill bizMerchBill){
        AjaxResult ajaxResult = AjaxResult.success("账单详情");
        if (bizMerchBill.getBillType().equals("1")){// 款项类型为1 为返现
            BizMerchBill bizMerchBill1 = bizMerchBillService.queryBillTypeOne(bizMerchBill);
            ajaxResult.put("data",bizMerchBill1);
        }
        if (bizMerchBill.getBillType().equals("2")){
            BizShareDetail bizShareDetail = bizMerchBillService.queryBillTypeTwo(bizMerchBill);
            ajaxResult.put("data",bizShareDetail);

        }
        if (bizMerchBill.getBillType().equals("4")){
            BizCashoutRecordsDTO bizCashoutRecords = bizMerchBillService.queryBillTypeFour(bizMerchBill);
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            bizCashoutRecords.setDepositTime(sf.format(bizCashoutRecords.getCreateTime()));
            // 截取银行卡后四位
            String account = bizCashoutRecords.getDealAccount().substring(bizCashoutRecords.getDealAccount().length()-4);
            String gatheringInfo = bizCashoutRecords.getDealBank()+"(尾号"+account+")";
            bizCashoutRecords.setDealBank(gatheringInfo);
            ajaxResult.put("data",bizCashoutRecords);
        }
        return ajaxResult;
    }


}
